package com.tlgen.zero.utils;

import java.io.*;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class FileUtils {

    /**
     * 读取文件内容
     *
     * @param file
     * @return
     */
    public static String readFile(File file) {
        StringBuilder text = new StringBuilder();
        String encoding = "utf-8";
        try (InputStreamReader read = new InputStreamReader(Files.newInputStream(file.toPath()), encoding);
             BufferedReader bufferedReader = new BufferedReader(read)) {
            //判断文件是否存在
            if (file.isFile() && file.exists()) {
                String lineTxt;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    text.append(lineTxt).append("\n");
                }
            } else {
                throw new RuntimeException("找不到指定的文件!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text.toString();
    }

    /**
     * 当前目录下的所有文件-不包括目录-递归方法
     *
     * @param fileList
     */
    public static void getAllFile(File file, List<File> fileList) {
        if (file.isDirectory()) {
            for (File listFile : Objects.requireNonNull(file.listFiles())) {
                getAllFile(listFile, fileList);
            }
        } else {
            fileList.add(file);
        }
    }

    /**
     * 当前目录下的所有文件-不包括目录-递归方法
     *
     * @param fileList
     */
    public static void getAllFile(List<File> fileList) {
        String localDirectory = System.getProperty("user.dir");
        File file = new File(localDirectory);
        if (file.isDirectory()) {
            for (File listFile : Objects.requireNonNull(file.listFiles())) {
                getAllFile(listFile, fileList);
            }
        } else {
            fileList.add(file);
        }
    }

    public static File getAFile(String className) {
        List<File> aFiles = new ArrayList<>();
        FileUtils.getAllFile(aFiles);
        return aFiles.stream()
                .filter(x -> {
                    String absolutePath = x.getAbsolutePath();
                    if (absolutePath.contains("\\src\\main\\java\\")
                            && absolutePath.contains("\\model\\")
                            && x.getName().contains(".java")) {
                        String xClassName = absolutePath.substring(absolutePath.lastIndexOf("\\") + 1,
                                absolutePath.lastIndexOf(".java"));
                        return Objects.equals(xClassName, StringUtils.cap(className));
                    }
                    return false;
                })
                .findFirst()
                .orElse(null);
    }

    public static String getFileCompleteClassName(File aFile) {
        if (Objects.nonNull(aFile)) {
            String absolutePath = aFile.getAbsolutePath();
            return absolutePath
                    .substring(absolutePath.indexOf("\\java") + 6, absolutePath.lastIndexOf(".java"))
                    .replaceAll("\\\\", ".");
        }
        return null;
    }

}
